<?php
class option{
	public static function get($name){
		global $db;
		
		if( isset($GLOBALS[$name]) )
			return $GLOBALS[$name];

		return $db->get_val("SELECT option_value FROM {$db->options} WHERE option_name='{$name}'");
	}

	public static function set($name, $value, $autoload = false){
		global $db;

		$db->get_val("SELECT option_value FROM {$db->options} WHERE option_name='{$name}'");
		if( $db->num_rows() > 0 )
			return false;
		
		$num = $db->insert($db->options, array(
			'option_name'  => $name,
			'option_value' => $value,
			'autoload'     => (bool)$autoload ? 1 : 0,
			));
		
		return $num>0;
	}

	public static function update($name, $value, $autoload = null){
		global $db;

		$db->get_val("SELECT option_value FROM {$db->options} WHERE option_name='{$name}'");
		if( $db->num_rows() == 0 )
			return false;

		$num = $db->update($db->options, array(
			'option_value' => $value,
			'autoload'     => (bool)$autoload ? 1 : 0,
			), array(
			'option_name'  => $name,
		));

		return $num>0;
	}

	public static function remove($name, $value=''){
		if( isset($value) && !empty($value) ){
			
		}

		$db->query();
	}
}
class field{
	public static function getConfig( $field_id, $key ){
		global $db;

		return $db->get_val("SELECT fconfig_value FROM {$db->field_config} WHERE fconfig_key='{$key}' AND field_id='{$field_id}'");
	}

	public static function setConfig( $field_id, $key, $value='' ){
		global $db;

		$db->get_val("SELECT fconfig_value FROM {$db->field_config} WHERE fconfig_key='{$key}' AND field_id='{$field_id}'");
		if( $db->num_rows() > 0 )
			return false;
		
		$num = $db->insert($db->options, array(
			'field_id'      => $field_id,
			'fconfig_key'   => $key,
			'fconfig_value' => $value,
			));
		
		return $num>0;
	}

	public static function updateConfig( $field_id, $key, $value='' ){
		global $db;

		$db->get_val("SELECT fconfig_value FROM {$db->field_config} WHERE fconfig_key='{$key}' AND field_id='{$field_id}'");
		if( $db->num_rows() == 0 )
			return false;
		
		$num = $db->insert($db->options, array(
			'fconfig_value' => $value
			), array(
			'field_id'      => $field_id,
			'fconfig_key'   => $key,
		));
		
		return $num>0;
	}
}
?>